<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <style>
        .wrap {
            position: fixed;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background: url('images/bg.jpg') no-repeat;
            background-size: 100% 100%;
        }
        
        .reg_title {
            margin: 100px auto 0;
            padding: 0;
            width: 172px;
            height: 42px;
            background: url('images/title.png')
        }
        
        .form_group {
            width: 360px;
            margin: 0px auto;
            text-align: center;
            margin-top: 30px;
            position: relative;
        }
        
        .form_group span {
            position: absolute;
            left: 128px;
            top: 50px;
            font-size: 14px;
            color: #ff0;
        }
        
        .form_group label {
            float: left;
            width: 100px;
            line-height: 44px;
            text-align: right;
            font-size: 14px;
            color: #fff;
        }
        
        .form_group input {
            width: 240px;
            height: 40px;
            text-indent: 15px;
            border: 1px solid #b3d4df;
            border-radius: 20px;
            outline: none;
            background-color: rgba(255, 255, 255, 0.2);
            color: #fff;
        }
        
        .form_group .submit {
            background-color: #4cb3d6;
            width: 150px;
            float: left;
            margin-left: 110px;
            border: 0px;
            text-indent: 0;
            cursor: pointer;
        }
    </style>
</head>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>

<body>
    <div class="wrap">
        <h2 class="reg_title"></h2>
        <div class="form_group">
            <label>用户名：</label>
            <input type="text" placeholder="请输入用注册的用户名" class="username" />
            <span class="info"></span>
        </div>
        <div class="form_group">
            <input type="button" value="注 册" class="submit" />
        </div>
    </div>
    <script>
        const btn = document.querySelector('.submit'); //点击注册
        const val = document.querySelector('.username'); //用户输入
        const ze = document.querySelector('.info'); //返回的内容
        // 检测是否被注册 
        btn.addEventListener('click', () => {
            ze.innerHTML = `注册中....`;
            $.ajax({
                url: 'https://autumnfish.cn/api/user/register',
                type: 'POST',
                data: {
                    username: val.value
                },
                success: function(data) {
                    ze.innerHTML = data
                }
            })
        });

        //用户失去焦点事件  失去焦点验证
        val.addEventListener('blur', () => {
            ze.innerHTML = `注册中....`;
            $.ajax({
                url: 'https://autumnfish.cn/api/user/check',
                type: 'POST',
                data: {
                    username: val.value
                },
                success: function(data) {
                    ze.innerHTML = data
                }
            })

        });
    </script>
</body>

</html>